/**
 * @Author wang
 * 	实例化Request请求类
 */
import Request from '@/utils/request/index.js'
// import store from '@/store/index.js'

const getTokenStorage = () => {
  let token = ''
  try {
    token = uni.getStorageSync('token')
  } catch (e) {
  }
  return token
}

const http = new Request()
 
/* 设置全局配置 */
http.setConfig((config) => { 
  config.baseURL = "http://1.116.9.185/api"
  config.header = {
    ...config.header,
    // a: 1
  }
  config.custom = {
    // auth: false, // 是否传token
    // loading: false // 是否使用loading
  }
  return config
})

 
/* request拦截器 */
http.interceptors.request.use((config) => {
	 let userInfo = uni.getStorageSync("userInfo")
	 // 'token': userInfo.token
	 console.log('token',userInfo.token)
	// let token = store.getters.token
	config.header = {
		...config.header,
		'Token': userInfo.token
		// Cookie:"PHPSESSID=" + userInfo.token
		// Authorization:"Bearer" + token
	}
  // console.log("token",token)
  return config
}, (config) => {
  return Promise.reject(config)
})

/* response拦截器。 */
http.interceptors.response.use((response) => { 
 
  if (response.data.code !== 1) {
    return Promise.reject(response)
  }
  return response
}, (response) => { 
	// 请求错误
	if (response.data.msg=="请登录后操作") {
		 setTimeout(()=>{
		 		  
		  uni.removeStorageSync("userInfo")
		  uni.reLaunch({
			url:'/pages/user/login'
		  })
		 },1500)
	}
  return Promise.reject(response)
})

export {
  http
}
